#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int n, m;
int arr[5050][5050];
int main()
{
	int value = 0, x = 0, y = 0;
	int max_x = 0, max_y = 0;
	max_x = max_y = 5010;
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++) {
		scanf("%d%d%d", &x, &y, &value);
		arr[x + 1][y + 1] += value;
		//max_x = max_x > x ? max_x : x;
		//max_y = max_y > y ? max_y : y;
	}
	for (int i = 1; i < max_x + 1; i++) {
		for (int j = 1; j < max_y + 1; j++) {
			arr[i][j] = arr[i][j] + arr[i][j - 1]
				+ arr[i - 1][j] - arr[i - 1][j - 1];
		}
	}
	int ans = 0;
	for (int i = m; i < max_x; i++) {
		for (int j = m; j < max_y; j++) {
			int tmp = arr[i][j] + arr[i - m][j - m] - arr[i - m][j] - arr[i][j - m];
			ans = max(ans, tmp);
		}
	}
	cout << ans << endl;
	return 0;
}